package com.leyou.item.web;

import com.leyou.common.dto.PageDTO;
import com.leyou.dto.SkuDTO;
import com.leyou.dto.SpecParamDTO;
import com.leyou.dto.SpuDTO;
import com.leyou.dto.SpuDetailDTO;
import com.leyou.entity.Sku;
import com.leyou.entity.Spu;
import com.leyou.entity.SpuDetail;
import com.leyou.item.service.SkuService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private SpuService spuService;
    @Autowired
    private SpuDetailService spuDetailService;
    @Autowired
    private SkuService skuService;

    @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>> queryBycategoryandId(
            @RequestParam(value = "brandId",required = false) Integer brandId,
            @RequestParam(value = "categoryId",required = false) Integer categoryId,
            @RequestParam(value = "id",required = false) Integer id,
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "5") Integer rows,
            @RequestParam(value = "saleable",required = false) Boolean saleable
    ) {
        return ResponseEntity.ok(spuService.queryBypage(brandId, categoryId, id, page, rows, saleable));

    }

    @PostMapping("/spu")
    public ResponseEntity<Void> InputCategory(@RequestBody SpuDTO spuDTO) {
        //新增商品
        spuService.inputcategory(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();

    }

    @PutMapping("/saleable")
    public ResponseEntity<Void> UpdateCategoryStatus(
            @RequestParam("id") Long id,
            @RequestParam("saleable") Boolean saleable
    ) {
        spuService.updatecatestatus(id, saleable);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @GetMapping("/{id}")
    public ResponseEntity<SpuDTO> queryBypathId(@PathVariable("id") Long id) {
        SpuDTO spuDTO = spuService.queryBypathId(id);
        return ResponseEntity.ok(spuDTO);

    }

    @PutMapping
    public ResponseEntity<Void> upadteCategoryTable(@RequestBody SpuDTO spuDTO) {
        spuService.updateCategory(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();

    }

    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> querySpuDetail(@RequestParam("id") Integer id) {
        SpuDetail byId = spuDetailService.getById(id);
        SpuDetailDTO spuDetailDTO = new SpuDetailDTO(byId);
        return ResponseEntity.ok(spuDetailDTO);
    }

    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySkulist(@RequestParam("id") Integer id) {
        List<Sku> spu_id = skuService.query().eq("spu_id", id).list();
        List<SkuDTO> skuDTOS = SkuDTO.convertEntityList(spu_id);
        return ResponseEntity.ok(skuDTOS);
    }

    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>> querySkuListByIds(@RequestParam("ids") List<Long> ids) {
        List<Sku> skus = skuService.listByIds(ids);
        List<SkuDTO> skuDTOS = SkuDTO.convertEntityList(skus);
        return ResponseEntity.ok(skuDTOS);
    }

    @GetMapping("/spec/value")
    public ResponseEntity<List<SpecParamDTO>> queryListparam(
            @RequestParam("id") Integer id,
            @RequestParam(value = "searching", required = false) Boolean searching) {
        List<SpecParamDTO> queryparams = spuDetailService.queryparams(id, searching);
        return ResponseEntity.ok(queryparams);
    }

    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> querySputable(@PathVariable("id") Integer id) {
        Spu byId = spuService.getById(id);
        SpuDTO spuDTO = new SpuDTO(byId);
        return ResponseEntity.ok(spuDTO);

    }


}
